package com.ovopark.firecar.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import androidx.core.content.ContextCompat
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.ovopark.firecar.R
import java.util.*

object QRCodeUtils {
    fun createQrCode(context: Context, content : String, width : Int, height : Int,
                     color1 : Int?, color2: Int?) : Bitmap {
        val hints =
            Hashtable<EncodeHintType, String?>()
        // 字符转码格式设置
        hints[EncodeHintType.CHARACTER_SET] = "utf-8"
        // 容错率设置
        hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.M.toString()
        // 空白边距设置
        hints[EncodeHintType.MARGIN] = "1"
        /** 2.将配置参数传入到QRCodeWriter的encode方法生成BitMatrix(位矩阵)对象  */
        val bitMatrix =
            QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints)

        /** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值  */
        val pixels = IntArray(width * height)
        for (y in 0 until height) {
            for (x in 0 until width) {
                //bitMatrix.get(x,y)方法返回true是黑色色块，false是白色色块
                if (bitMatrix[x, y]) {
                    if (StringUtils.isBlank(color1.toString())){
                        pixels[y * width + x] = ContextCompat.getColor(context, R.color.black) //黑色色块像素设置，可以通过传入不同的颜色实现彩色二维码，例如Color.argb(1,55,206,141)等设置不同的颜色。
                    }else{
                        color1?.let {
                            pixels[y * width + x] = ContextCompat.getColor(context,it)

                        }
                    }
                } else {
                    if (StringUtils.isBlank(color1.toString())){
                        pixels[y * width + x] =  ContextCompat.getColor(context,R.color.white) // 白色色块像素设置
                    }else{
                        color1?.let {
                            pixels[y * width + x] = ContextCompat.getColor(context,it)
                        }
                    }

                }
            }
        }
        val bitmap : Bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888)
        bitmap.setPixels(pixels,0,width,0,0,width,height)
        return bitmap
    }

    /***
     * 为二维码添加logo
     *
     * @param srcBitmap 二维码图片
     * @param logoBitmap logo图片
     * @param percent logo比例
     * @return 生成的最终的图片
     */
    fun addLogo(srcBitmap: Bitmap?, logoBitmap: Bitmap?, percent: Float): Bitmap? {
        //判断参数是否正确
        if (srcBitmap == null)
            return null
        if (logoBitmap == null)
            return srcBitmap
        //输入logo图片比例错误自动纠正为默认的0.2f
        var logoPercent = percent
        if (percent < 0 || percent > 1)
            logoPercent = 0.2f

        //分别获取bitmap图片的大小
        var sHeight = srcBitmap.height
        var sWidth = srcBitmap.width
        var lHeight = logoBitmap.height
        var lWidth = logoBitmap.width

        //获取缩放比例
        var scareWidth = sHeight * logoPercent / lWidth
        var scareHeight = sWidth * logoPercent / lHeight

        //使用canvas重新绘制bitmap
        var bitmap = Bitmap.createBitmap(sWidth, sHeight, Bitmap.Config.ARGB_8888)
        var canvas = Canvas(bitmap)
        canvas.drawBitmap(srcBitmap, 0f, 0f, null)
        canvas.scale(
            scareWidth,
            scareHeight,
            (sWidth / 2).toFloat(),
            (sHeight / 2).toFloat()
        )   //设置缩放中心基点
        canvas.drawBitmap(
            logoBitmap,
            (sWidth/2 - lWidth / 2).toFloat(),
            (sHeight/2 - lHeight / 2).toFloat(),
            null
        )
        return bitmap
    }

}